package com.mzq.langchain4j_springboot.web.controller;


import com.mzq.langchain4j_springboot.config.AiConfig;
import com.mzq.langchain4j_springboot.web.service.ISysUserService;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.service.TokenStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.time.LocalDate;


/**
 * 用户信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController
{
//    @Resource
//    private ISysUserService userService;
    /**
     * 土地房产api
     */
    @Autowired
    AiConfig.AssistantUnique assistantUnique;

    /**
     * 隐患排查api
     */
    @Autowired
    AiConfig.EquityAssistant equityAssistant;

//    @Autowired
//    ISysUserService iSysUserService;

    /**
     * 获取用户列表
     */
//    @GetMapping("/list")
//    public List<SysUser> list(SysUser user)
//    {
//        return userService.selectUserList(user);
//    }

    /**
     * 根据用户id区分对话内容
     * @param message
     * @param userId
     * @return
     */
    @RequestMapping(value = "/memoryId_chat")
    public String memoryChat(@RequestParam(name ="message" ,defaultValue="我是谁") String message, @RequestParam(name ="userId")Integer userId) {
//        String chat = assistantUnique.chat(userId, message);
        return assistantUnique.chat(message);
    }

    @RequestMapping(value = "/memoryId_chatFlux")
    public Flux<ServerSentEvent<String>> memoryChatStream(@RequestParam(name ="message" ,defaultValue="我是谁") String message, @RequestParam(name ="userId")Integer userId) {
//        String chat = assistantUnique.chat(userId, message);
        Flux<ServerSentEvent<String>> map = assistantUnique.stream(userId, message)
                .map(chunk -> ServerSentEvent.<String>builder()
                        .data(chunk)
                        .build());
        return map;
    }

    @RequestMapping(value = "/memoryId_chatStream")
    public Flux<ServerSentEvent<String>> chatStream(@RequestParam(name ="message" ,defaultValue="我是谁") String message, @RequestParam(name ="userId")Integer userId) {
//        String chat = assistantUnique.chat(userId, message);
        Flux<ServerSentEvent<String>> map = assistantUnique.chatStream(userId, message)
                .map(chunk -> ServerSentEvent.<String>builder()
                        .data(chunk)
                        .build());
        return map;
    }


//    @RequestMapping(value = "/string_chat")
//    public String string_chat() {
////        String chat = assistantUnique.chat(userId, message);
//        return iSysUserService.selectRealEstateInformation("天津市津燃置业投资有限公司");
//    }


//    @RequestMapping(value = "/memory_stream_chat_pw",produces ="text/stream;charset=UTF-8")
//    public Flux<String> memoryStreamChatByPW(@RequestParam(name="message",defaultValue="我是谁") String message, HttpServletResponse response) {
//
////    public String memoryStreamChatByPW(@RequestParam(name="message",defaultValue="我是谁") String message, HttpServletResponse response) {
//        TokenStream stream = equityAssistant.stream(message, LocalDate.now().toString());
//        System.out.println("走了吗");
//        Flux<Object> objectFlux = Flux.create(sink -> {
//            stream.onPartialResponse(s -> sink.next(s))
//                    .onCompleteResponse(c -> sink.complete())
//                    .onError(sink::error)
//                    .start();
//
//        }) ;
//        return Flux.create(sink -> {
//            stream.onPartialResponse(s -> sink.next(s))
//                    .onCompleteResponse(c -> sink.complete())
//                    .onError(sink::error)
//                    .start();
//
//        });
//
////        return equityAssistant.chat(message);
//    }
//
//
//    @RequestMapping(value = "/memory_stream_chat_rl",produces ="text/stream;charset=UTF-8")
//    public Flux<String> memoryStreamChatByRL(@RequestParam(name="message",defaultValue="我是谁") String message, HttpServletResponse response) {
////        MultiModalConversation conv = new MultiModalConversation();
////        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
////                .content(Arrays.asList(
////                        Collections.singletonMap("image", "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"),
////                        Collections.singletonMap("image", "https://dashscope.oss-cn-beijing.aliyuncs.com/images/tiger.png"),
////                        Collections.singletonMap("image", "https://dashscope.oss-cn-beijing.aliyuncs.com/images/rabbit.png"),
////                        Collections.singletonMap("text", "这些是什么?"))).build();
//        //拼接千问vl模型所需输入体
//        UserMessage userMessage = UserMessage.from(
//                TextContent.from("图片存在什么安全隐患"),
//                ImageContent.from("https://tjnykjgs.oss-cn-beijing.aliyuncs.com/15087bcf-755a-47d9-affc-7fef6dc5bfdc.png")
//        );
////    public String memoryStreamChatByPW(@RequestParam(name="message",defaultValue="我是谁") String message, HttpServletResponse response) {
//        TokenStream stream = equityAssistant.streamByRL("asd", LocalDate.now().toString());
//        System.out.println("走了吗11111");
//        return Flux.create(sink -> {
//            stream.onPartialResponse(s -> sink.next(s))
//                    .onCompleteResponse(c -> sink.complete())
//                    .onError(sink::error)
//                    .start();
//
//        });
//
////        return equityAssistant.chat(message);
//    }
}
